<?php require_once('../Connections/fastfood_con.php'); ?>
<?php
//MX Widgets3 include
require_once('../includes/wdg/WDG.php');

// Load the common classes
require_once('../includes/common/KT_common.php');

// Load the tNG classes
require_once('../includes/tng/tNG.inc.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("../");

// Make unified connection variable
$conn_fastfood_con = new KT_connection($fastfood_con, $database_fastfood_con);

//start Trigger_CheckPasswords trigger
//remove this line if you want to edit the code by hand
function Trigger_CheckPasswords(&$tNG) {
  $myThrowError = new tNG_ThrowError($tNG);
  $myThrowError->setErrorMsg("Passwords do not match.");
  $myThrowError->setField("my_pass");
  $myThrowError->setFieldErrorMsg("The two passwords do not match.");
  return $myThrowError->Execute();
}
//end Trigger_CheckPasswords trigger

//start Trigger_WelcomeEmail trigger
//remove this line if you want to edit the code by hand
function Trigger_WelcomeEmail(&$tNG) {
  $emailObj = new tNG_Email($tNG);
  $emailObj->setFrom("{KT_defaultSender}");
  $emailObj->setTo("{my_mail}");
  $emailObj->setCC("");
  $emailObj->setBCC("");
  $emailObj->setSubject("Welcome");
  //FromFile method
  $emailObj->setContentFile("../includes/mailtemplates/welcome.html");
  $emailObj->setEncoding("ISO-8859-1");
  $emailObj->setFormat("HTML/Text");
  $emailObj->setImportance("Normal");
  return $emailObj->Execute();
}
//end Trigger_WelcomeEmail trigger

// Start trigger
$formValidation1 = new tNG_FormValidation();
$formValidation1->addField("my_mail", true, "text", "email", "", "", "");
$tNGs->prepareValidation($formValidation1);
// End trigger

//start Trigger_ForgotPassword_Email trigger
//remove this line if you want to edit the code by hand
function Trigger_ForgotPassword_Email(&$tNG) {
  $emailObj = new tNG_Email($tNG);
  $emailObj->setFrom("{KT_defaultSender}");
  $emailObj->setTo("{my_mail}");
  $emailObj->setCC("");
  $emailObj->setBCC("");
  $emailObj->setSubject("Forgot password email");
  //FromFile method
  $emailObj->setContentFile("../includes/mailtemplates/forgot.html");
  $emailObj->setEncoding("ISO-8859-1");
  $emailObj->setFormat("HTML/Text");
  $emailObj->setImportance("Normal");
  return $emailObj->Execute();
}
//end Trigger_ForgotPassword_Email trigger

// Start trigger
$formValidation = new tNG_FormValidation();
$formValidation->addField("my_user", true, "text", "", "", "", "");
$formValidation->addField("my_pass", true, "text", "", "", "", "");
$formValidation->addField("my_mail", true, "text", "email", "", "", "");
$tNGs->prepareValidation($formValidation);
// End trigger

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

//start Trigger_ForgotPasswordCheckEmail trigger
//remove this line if you want to edit the code by hand
function Trigger_ForgotPasswordCheckEmail(&$tNG) {
  return Trigger_ForgotPassword_CheckEmail($tNG);
}
//end Trigger_ForgotPasswordCheckEmail trigger

mysql_select_db($database_fastfood_con, $fastfood_con);
$query_Recordset1 = "SELECT * FROM my_admin";
$Recordset1 = mysql_query($query_Recordset1, $fastfood_con) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

// Make an insert transaction instance
$userRegistration = new tNG_insert($conn_fastfood_con);
$tNGs->addTransaction($userRegistration);
// Register triggers
$userRegistration->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Insert1");
$userRegistration->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$userRegistration->registerTrigger("END", "Trigger_Default_Redirect", 99, "{kt_login_redirect}");
$userRegistration->registerConditionalTrigger("{POST.my_pass} != {POST.re_my_pass}", "BEFORE", "Trigger_CheckPasswords", 50);
$userRegistration->registerTrigger("AFTER", "Trigger_WelcomeEmail", 40);
// Add columns
$userRegistration->setTable("my_admin");
$userRegistration->addColumn("my_name", "STRING_TYPE", "POST", "my_name");
$userRegistration->addColumn("my_user", "STRING_TYPE", "POST", "my_user");
$userRegistration->addColumn("my_pass", "STRING_TYPE", "POST", "my_pass");
$userRegistration->addColumn("my_mail", "STRING_TYPE", "POST", "my_mail");
$userRegistration->setPrimaryKey("id", "NUMERIC_TYPE");

// Make an update transaction instance
$forgotpass_transaction = new tNG_update($conn_fastfood_con);
$tNGs->addTransaction($forgotpass_transaction);
// Register triggers
$forgotpass_transaction->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Update1");
$forgotpass_transaction->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation1);
$forgotpass_transaction->registerTrigger("BEFORE", "Trigger_ForgotPasswordCheckEmail", 20);
$forgotpass_transaction->registerTrigger("AFTER", "Trigger_ForgotPassword_Email", 1);
$forgotpass_transaction->registerTrigger("END", "Trigger_Default_Redirect", 99, "{kt_login_redirect}");
// Add columns
$forgotpass_transaction->setTable("my_admin");
$forgotpass_transaction->addColumn("my_mail", "STRING_TYPE", "POST", "my_mail");
$forgotpass_transaction->setPrimaryKey("my_mail", "STRING_TYPE", "POST", "my_mail");

// Execute all the registered transactions
$tNGs->executeTransactions();

// Get the transaction recordset
$rsmy_admin = $tNGs->getRecordset("my_admin");
$row_rsmy_admin = mysql_fetch_assoc($rsmy_admin);
$totalRows_rsmy_admin = mysql_num_rows($rsmy_admin);
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['textfield'])) {
  $loginUsername=$_POST['textfield'];
  $password=$_POST['textfield'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "admin_cpanel.php";
  $MM_redirectLoginFailed = "login.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_fastfood_con, $fastfood_con);
  
  $LoginRS__query=sprintf("SELECT my_user, my_pass FROM my_admin WHERE my_user=%s AND my_pass=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $fastfood_con) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wdg="http://ns.adobe.com/addt">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="../includes/common/js/sigslot_core.js"></script>
<script src="../includes/common/js/base.js" type="text/javascript"></script>
<script src="../includes/common/js/utility.js" type="text/javascript"></script>
<script type="text/javascript" src="../includes/wdg/classes/MXWidgets.js"></script>
<script type="text/javascript" src="../includes/wdg/classes/MXWidgets.js.php"></script>
<script type="text/javascript" src="../includes/wdg/classes/JSRecordset.js"></script>
<script type="text/javascript" src="../includes/wdg/classes/DynamicInput.js"></script>
<?php
//begin JSRecordset
$jsObject_Recordset1 = new WDG_JsRecordset("Recordset1");
echo $jsObject_Recordset1->getOutput();
//end JSRecordset
?>
<link href="../includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<script src="../includes/skins/style.js" type="text/javascript"></script>
<?php echo $tNGs->displayValidationRules();?>
</head>

<body>
<form id="form1" name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
  <p>
    <label>
      <input type="text" name="textfield" id="textfield" />
    </label>
  </p>
  <p>
    <label>
      <?php
  echo $tNGs->getLoginMsg();
?>
      <input name="textfield2" id="textfield2" wdg:recordset="Recordset1" wdg:subtype="DynamicSearch" wdg:type="widget" wdg:displayfield="my_user" wdg:norec="50" wdg:defaultoptiontext="no" />
    </label>
  </p>
</form>
<?php
	echo $tNGs->getErrorMsg();
?>
<form method="post" id="form2" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">
<table cellpadding="2" cellspacing="0" class="KT_tngtable">
  <tr>
    <td class="KT_th"><label for="my_name">My_name:</label></td>
    <td><input type="text" name="my_name" id="my_name" value="<?php echo KT_escapeAttribute($row_rsmy_admin['my_name']); ?>" size="32" />
      <?php echo $tNGs->displayFieldHint("my_name");?> <?php echo $tNGs->displayFieldError("my_admin", "my_name"); ?></td>
  </tr>
  <tr>
    <td class="KT_th"><label for="my_user">My_user:</label></td>
    <td><input type="text" name="my_user" id="my_user" value="<?php echo KT_escapeAttribute($row_rsmy_admin['my_user']); ?>" size="32" />
      <?php echo $tNGs->displayFieldHint("my_user");?> <?php echo $tNGs->displayFieldError("my_admin", "my_user"); ?></td>
  </tr>
  <tr>
    <td class="KT_th"><label for="my_pass">My_pass:</label></td>
    <td><input type="password" name="my_pass" id="my_pass" value="" size="32" />
      <?php echo $tNGs->displayFieldHint("my_pass");?> <?php echo $tNGs->displayFieldError("my_admin", "my_pass"); ?></td>
  </tr>
  <tr>
    <td class="KT_th"><label for="re_my_pass">Re-type My_pass:</label></td>
    <td><input type="password" name="re_my_pass" id="re_my_pass" value="" size="32" /></td>
  </tr>
  <tr>
    <td class="KT_th"><label for="my_mail">My_mail:</label></td>
    <td><input type="text" name="my_mail" id="my_mail" value="<?php echo KT_escapeAttribute($row_rsmy_admin['my_mail']); ?>" size="32" />
      <?php echo $tNGs->displayFieldHint("my_mail");?> <?php echo $tNGs->displayFieldError("my_admin", "my_mail"); ?></td>
  </tr>
  <tr class="KT_buttons">
    <td colspan="2"><input type="submit" name="KT_Insert1" id="KT_Insert1" value="Register" /></td>
  </tr>
</table>
<?php
	echo $tNGs->getErrorMsg();
?>
<form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">
  <table cellpadding="2" cellspacing="0" class="KT_tngtable">
    <tr>
      <td class="KT_th"><label for="my_mail"> Email:</label></td>
      <td><input type="text" name="my_mail" id="my_mail" value="<?php echo KT_escapeAttribute($row_rsmy_admin['my_mail']); ?>" size="32" />
        <?php echo $tNGs->displayFieldHint("my_mail");?> <?php echo $tNGs->displayFieldError("my_admin", "my_mail"); ?></td>
    </tr>
    <tr class="KT_buttons">
      <td colspan="2"><input type="submit" name="KT_Update1" id="KT_Update1" value="Submit" /></td>
    </tr>
  </table>
</form>
<p>&nbsp;</p>
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
